﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
public class Full_Process
{
    public long id { get; set; }
    public String name { get; set; }
    public String description { get; set; }
    public List<Process_Stages> stages { get; set; }
}
public class Process_Stages
{
    public long id { get; set; }
    public String name { get; set; }
    public String observation { get; set; }
    public int temperature { get; set; }
    public int time { get; set; }
    public int water { get; set; }
    public int order { get; set; }
    public List<Stage_Products> products { get; set; }
}
public class Stage_Products
{
    public long id { get; set; }
    public String name { get; set; }
    public decimal quantity { get; set; }
    public String observation { get; set; }
}
public class BLLProcess
{
    DatabaseDataContext Context;

    public BLLProcess()
	{
        Context = new DatabaseDataContext();
	}
    public List<Process> GetProcesses(string sortExp, string sortDirection, int pageIndex, int filterStatus, String filterSearch, int startRow,
        int pageSize, string sortColumns)
    {
        List<Process> list = new List<Process>();
        try
        {
            sortColumns = sortExp + " " + sortDirection;
            startRow = pageIndex * pageSize;
            var items = (from p in Context.Processes select p);
            if (!String.IsNullOrEmpty(filterSearch))
            {
                items = items.Where(l => l.Name.Contains(filterSearch));
            }
            if (filterStatus > 0)
            {
                bool statusB = false;
                switch (filterStatus)
                {
                    case 1:
                        statusB = true;
                        break;
                    default:
                        statusB = false;
                        break;
                }
                items = items.Where(l => l.IsEnabled == statusB);
            }
            if (!String.IsNullOrEmpty(sortColumns))
            {
                if (sortColumns.Contains("DESC"))
                {
                    if (sortColumns.Contains("Name"))
                        items = items.OrderByDescending(l => l.Name);
                    if (sortColumns.Contains("isEnabled"))
                        items = items.OrderByDescending(l => l.IsEnabled);
                }
                else
                {
                    if (sortColumns.Contains("Name"))
                        items = items.OrderBy(l => l.Name);
                    if (sortColumns.Contains("isEnabled"))
                        items = items.OrderBy(l => l.IsEnabled);
                }
            }
            list = items.Skip(startRow).Take(pageSize).ToList();
        }
        catch
        {}
        if (list.Count == 0)
        {
            Process p = new Process();
            p.Id = -1;
            p.SampleImage = " ";
            list.Add(p);
        }
        return list;
    }
    public int GetProcessesCount(string sortExp, string sortDirection, int pageIndex, int filterStatus, String filterSearch)
    {
        int count = 0;

        try
        {
            var items = (from p in Context.Processes select p);
            if (!String.IsNullOrEmpty(filterSearch))
            {
                items = items.Where(l => l.Name.Contains(filterSearch));
            }
            if (filterStatus > 0)
            {
                bool statusB = false;
                switch (filterStatus)
                {
                    case 1:
                        statusB = true;
                        break;
                    default:
                        statusB = false;
                        break;
                }
                items = items.Where(l => l.IsEnabled == statusB);
            }
            count = items.Count();
        }
        catch
        { }
        if (count == 0)
        {
            count = 1;
        }
        return count;
    }
    public bool Save(Process pr)
    {
        bool result = false;
        try
        {
            Process process = pr;
            if(pr.Id > 0){
                process = (from p in Context.Processes where p.Id == pr.Id select p).First();
                process.Name = pr.Name;
                process.Description = pr.Description;
                process.IsEnabled = pr.IsEnabled;
                process.SampleImage = pr.SampleImage;
            }
            else
            {
                Context.Processes.InsertOnSubmit(pr);
            }
            Context.SubmitChanges();
            result = true;
        }
        catch
        {
        }
        return result;
    }
    public bool UpdateStatus(long Id)
    {
        bool result = false;
        try
        {
            Process pr = (from p in Context.Processes where p.Id == Id select p).First();
            if (pr != null)
            {
                pr.IsEnabled = !pr.IsEnabled;
                Context.SubmitChanges();
                result = true;
            }
        }
        catch
        {
        }
        return result;
    }
    public Process getProcessById(long Id)
    {
        Process process = null;
        try
        {
            process = (from p in Context.Processes where p.Id == Id select p).First();            
        }
        catch
        {
        }
        return process;
    }
    public Full_Process getFullProcessById(long Id)
    {
        Full_Process process = null;
        try
        {
            var pr = (from p in Context.Processes where p.Id == Id select p).First();
            if (pr != null)
            {
                process = new Full_Process();
                process.name = pr.Name;
                process.id = pr.Id;
                process.description = pr.Description;
                List<Process_Stages> stages = new List<Process_Stages>();
                process.stages = stages;
                foreach (Stage s in pr.Stages)
                {
                    Process_Stages ps = new Process_Stages();
                    ps.id = s.Id;
                    ps.name = s.Name;
                    ps.observation = s.Observation;
                    ps.temperature = s.Temperature;
                    ps.time = s.Time;
                    ps.water = (s.Water==null)?0:Convert.ToInt32(s.Water);
                    ps.order = (s.Order==null)?0:Convert.ToInt32(s.Order);
                    List<Stage_Products> products = new List<Stage_Products>();
                    ps.products = products;
                    stages.Add(ps);
                    foreach (StageDetail sd in s.StageDetails)
                    {
                        Stage_Products product = new Stage_Products();
                        product.id = sd.Id;
                        product.name = sd.Product.Name;
                        product.observation = sd.Observation;
                        product.quantity = sd.Quantity;
                        products.Add(product);
                    }
                }
                stages = (from s in stages select s).OrderBy(o => o.order).ToList();
                process.stages = stages;
            }
        }
        catch
        {
        }
        return process;
    }
    public List<Process> GetProcesses()
    {
        List<Process> list = new List<Process>();
        try
        {
            list = (from p in Context.Processes where p.IsEnabled == true select p).OrderBy(p => p.Name).ToList();
        }
        catch
        { }
        return list;
    }
}